// hospital/pages/registrationDetails/registrationDetails.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospitalName: app.globalData.hospitalName,
    baseUrl: app.globalData.path,
    departmant: '', //所选科室信息
    dayList: [],
    selectIndex: -1, //  选择的日期
    nowTime: '',
    doctorList: [],
    selectDoctorIndex: -1, //所选医生索引
    selectTimeIndex: -1, //  所选时间段索引
    timeList: [], //可预约时间段
  },


  // 获取科室医生列表
  getDoctorList() {
    var url = app.globalData.url + "doctor/listDoctorByDepart",
      baseUrl = this.data.baseUrl,
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        departmentId: that.data.departmant.departmentId
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          var datas = res.data
          that.setData({
            doctorList: datas
          })
          console.log(datas);
        }
      },
      err => {
        console.log(err);
      })




  },

  avatarError(e) {
    var index = e.currentTarget.dataset.index,
      doctorList = this.data.doctorList;
    doctorList[index].doctorPic = '/img/userImg.jpg'
    console.log(e);
    this.setData({
      doctorList
    })
  },

  // 选择医生
  selectDoctor(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      selectDoctorIndex: index == this.data.selectDoctorIndex ? -1 : index
    })

    this.getHaoList()


  },
  // 选择时间段
  selectTime(e) {
    var index = e.currentTarget.dataset.index;


    this.setData({
      selectTimeIndex: index == this.data.selectTimeIndex ? -1 : index
    })
  },

  // 选择挂号日期
  selectDate(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    this.setData({
      selectIndex: index
    })

    this.getHaoList()
  },

  getHaoList() {


    if (this.data.selectIndex == -1 || this.data.selectDoctorIndex == -1) {
      return
    }
    var selectDay = this.data.dayList[this.data.selectIndex],
      selectTime = this.data.timeList[this.data.selectTimeIndex],
      doctorInfo = this.data.doctorList[this.data.selectDoctorIndex],
      departmantId = this.data.departmant.departmentId;
    console.log(selectDay);
    console.log(departmantId);
    console.log(selectTime);
    console.log(doctorInfo);

    var url = app.globalData.url + "doctor/listDoctorTimes",
      timeList = [],
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        doctorId: doctorInfo.doctorId,
        weekNum: selectDay.weekNum,
        dateTime: selectDay.label,
        userId: app.globalData.userId
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          var datas = res.data
          console.log(datas);

          datas.times.forEach(item => {
            if (item.SettingNum != 0 && item.isRegister != 1) {
              item.isSelect = false
              timeList.push(item)
            }
          })

          that.setData({
            timeList
          })
          console.log(that.data);
        }
      },
      err => {
        console.log(err);
      })
  },


  submitHandle() {

    var selectDay = this.data.dayList[this.data.selectIndex],
      selectTime = this.data.timeList[this.data.selectTimeIndex],
      doctorInfo = this.data.doctorList[this.data.selectDoctorIndex],
      url = app.globalData.url + "doctor/addUserRegister",
      that = this,
      departmant = this.data.departmant;

    console.log(doctorInfo);
    console.log(selectDay);
    console.log(selectTime);

    // return

    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        doctorId: doctorInfo.doctorId,
        weekNum: selectDay.weekNum,
        registerDate: selectDay.label,
        timeSlot: selectTime.timeSlot,
        userId: app.globalData.userId,
        departmentId: departmant.departmentId,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          wx.showModal({
            title: '您好',
            content: '您已成功预约，请准时就诊',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                that.setData({
                  selectTimeIndex: -1
                })
                that.getHaoList()
              }
            }
          })
        }
      },
      err => {
        console.log(err);
      })





  },


  getNow: function () {
    var date = new Date()
    var year = date.getFullYear()
    var month = this.add0(date.getMonth() + 1)
    var date = this.add0(date.getDate())
    var str = year + '-' + month + '-' + date
    this.getDate(str);

    this.setData({
      nowTime: year + '.' + month
    })
  },

  getDate: function (str) {
    console.log(str);
    var that = this
    let now = new Date(); // 
    let day = now.getDay(); //判断今天是周几
    let oneDayTime = 24 * 60 * 60 * 1000; //一天的时间
    let nowTime = Date.parse(str.replace(/-/g, "/") + ' ' + '00' + ':' + '00' + ':' + '00'); // 当天的时间戳
    var list = []
    for (var i = 0; i < 7; i++) {
      list[i] = {
        date: that.format(nowTime + i * oneDayTime, 0),
        day: that.getDay(nowTime + i * oneDayTime),
        weekNum: that.getDayNum(nowTime + i * oneDayTime),
        label: that.format(nowTime + i * oneDayTime, 1)
      }
    }
    console.log(list);

    this.setData({
      dayList: list
    })
  },
  add0: function (m) {
    return m < 10 ? '0' + m : m
  },
  format: function (shijianchuo, id) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = this.add0(time.getMonth() + 1);
    var d = this.add0(time.getDate());
    return id == 0 ? d : y + '-' + m + '-' + d;
  },
  getDay: function (str) {
    let day = new Date(str).getDay();
    switch (day) {
      case 0:
        return `周日`;
        break;
      case 1:
        return `周一`;
        break;
      case 2:
        return `周二`;
        break;
      case 3:
        return `周三`;
        break;
      case 4:
        return `周四`;
        break;
      case 5:
        return `周五`;
        break;
      case 6:
        return `周六`;
        break;
    }
  },
  getDayNum: function (str) {
    let day = new Date(str).getDay();
    switch (day) {
      case 0:
        return `7`;
        break;
      case 1:
        return `1`;
        break;
      case 2:
        return `2`;
        break;
      case 3:
        return `3`;
        break;
      case 4:
        return `4`;
        break;
      case 5:
        return `5`;
        break;
      case 6:
        return `6`;
        break;
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log(options);
    this.getNow()

    if (options.departmant != undefined) {
      console.log(11);
      this.setData({
        departmant: JSON.parse(options.departmant)
      })
      this.getDoctorList()
    } else {
      var doctorList = [],
        doctor = JSON.parse(options.doctor),
        departmant = {};
      doctorList.push(doctor)
      departmant.departmentId = doctor.departmentId
      this.setData({
        doctorList,
        selectDoctorIndex: 0,
        departmant
      })
    }



    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})